home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ArcTest.java < prev    next >
Text File  |  1998-09-15  |  5KB  |  162 lines

  1. /*
  2.  * @(#)ArcTest.java    1.4 97/02/05
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.applet.*;
  34.  
  35. /**
  36.  * An interactive test of the Graphics.drawArc and Graphics.fillArc
  37.  * routines. Can be run either as a standalone application by
  38.  * typing "java ArcTest" or as an applet in the AppletViewer.
  39.  */
  40. public class ArcTest extends Applet {
  41.     ArcControls controls;   // The controls for marking and filling arcs
  42.     ArcCanvas canvas;       // The drawing area to display arcs
  43.  
  44.     public void init() {
  45.     setLayout(new BorderLayout());
  46.     canvas = new ArcCanvas();
  47.     add("Center", canvas);
  48.     add("South", controls = new ArcControls(canvas));
  49.     }
  50.  
  51.     public void destroy() {
  52.         remove(controls);
  53.         remove(canvas);
  54.     }
  55.  
  56.     public void start() {
  57.     controls.setEnabled(true);
  58.     }
  59.  
  60.     public void stop() {
  61.     controls.setEnabled(false);
  62.     }
  63.   
  64.     public void processEvent(AWTEvent e) {
  65.         if (e.getID() == Event.WINDOW_DESTROY) {
  66.             System.exit(0);
  67.         }
  68.     }
  69.  
  70.     public static void main(String args[]) {
  71.     Frame f = new Frame("ArcTest");
  72.     ArcTest    arcTest = new ArcTest();
  73.  
  74.     arcTest.init();
  75.     arcTest.start();
  76.  
  77.     f.add("Center", arcTest);
  78.     f.setSize(300, 300);
  79.     f.show();
  80.     }
  81.  
  82.     public String getAppletInfo() {
  83.         return "An interactive test of the Graphics.drawArc and \nGraphics.fillArc routines. Can be run \neither as a standalone application by typing 'java ArcTest' \nor as an applet in the AppletViewer.";
  84.     }
  85. }
  86.  
  87. class ArcCanvas extends Canvas {
  88.     int        startAngle = 0;
  89.     int        endAngle = 45;
  90.     boolean    filled = false;
  91.     Font    font;
  92.  
  93.     public void paint(Graphics g) {
  94.     Rectangle r = getBounds();
  95.     int hlines = r.height / 10;
  96.     int vlines = r.width / 10;
  97.  
  98.     g.setColor(Color.pink);
  99.     for (int i = 1; i <= hlines; i++) {
  100.         g.drawLine(0, i * 10, r.width, i * 10);
  101.     }
  102.     for (int i = 1; i <= vlines; i++) {
  103.         g.drawLine(i * 10, 0, i * 10, r.height);
  104.     }
  105.  
  106.     g.setColor(Color.red);
  107.     if (filled) {
  108.         g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  109.     } else {
  110.         g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
  111.     }
  112.  
  113.     g.setColor(Color.black);
  114.     g.setFont(font);
  115.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  116.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  117.     g.drawLine(0, 0, r.width, r.height);
  118.     g.drawLine(r.width, 0, 0, r.height);
  119.     int sx = 10;
  120.     int sy = r.height - 28;
  121.     g.drawString("S = " + startAngle, sx, sy); 
  122.     g.drawString("E = " + endAngle, sx, sy + 14); 
  123.     }
  124.  
  125.     public void redraw(boolean filled, int start, int end) {
  126.     this.filled = filled;
  127.     this.startAngle = start;
  128.     this.endAngle = end;
  129.     repaint();
  130.     }
  131. }
  132.  
  133. class ArcControls extends Panel
  134.                   implements ActionListener {
  135.     TextField s;
  136.     TextField e;
  137.     ArcCanvas canvas;
  138.  
  139.     public ArcControls(ArcCanvas canvas) {
  140.     Button b = null;
  141.  
  142.     this.canvas = canvas;
  143.     add(s = new TextField("0", 4));
  144.     add(e = new TextField("45", 4));
  145.     b = new Button("Fill");
  146.     b.addActionListener(this);
  147.     add(b);
  148.     b = new Button("Draw");
  149.     b.addActionListener(this);
  150.     add(b);
  151.     }
  152.  
  153.     public void actionPerformed(ActionEvent ev) {
  154.     String label = ev.getActionCommand();
  155.  
  156.     canvas.redraw(label.equals("Fill"),
  157.                   Integer.parseInt(s.getText().trim()),
  158.                   Integer.parseInt(e.getText().trim()));
  159.     }
  160. }
  161.     
  162.